<template>
  <div :class="{ hidden: hidden }" id="pagination-container">
    <el-pagination
        :background="background"
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :layout="layout"
        :page-sizes="pageSizes"
        :total="Number(total)"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
  </div>
</template>

<script lang="ts" setup name='Pagination'>
import {} from 'vue'

let props = defineProps({
  total: {
    required: true,
    type: Number,
  },
  page: {
    type: Number,
    default: 1,
  },
  limit: {
    type: Number,
    default: 10,
  },
  pageSizes: {
    type: Array,
    default() {
      return [10, 20, 30, 50]
    },
  },
  layout: {
    type: String,
    default: 'total, sizes, prev, pager, next, jumper',
  },
  background: {
    type: Boolean,
    default: true,
  },
  autoScroll: {
    type: Boolean,
    default: true,
  },
  hidden: {
    type: Boolean,
    default: false,
  },
});

let emits = defineEmits<{
  (event: 'update:page', p: number): void;
  (event: 'update:limit', s: number): void;
  (event: 'pagination'): void;
}>();

const currentPage = computed({
  get() {
    return props.page
  },
  set(newVal) {
    emits('update:page', newVal)
  }
})
const pageSize = computed({
  get() {
    return props.limit
  },
  set(newVal) {
    emits('update:limit', newVal)
  }
})

const handleSizeChange = () => {
  emits('pagination')
}
const handleCurrentChange = () => {
  emits('pagination')
}

</script>

<style scoped lang='less'>
#pagination-container {
  background: #fff;
  margin-top: 10px;
  padding: 16px 16px;

  .hidden {
    display: none;
  }
}

</style>
